package com.ihome.excel.xlsx.config;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ihome.excel.xlsx.entity.Column;
import com.ihome.excel.xlsx.entity.ExcelRule;
import com.ihome.excel.xlsx.exceptions.ExcelRuleParseException;
import com.ihome.excel.xlsx.tool.XmlUtils;

public class ExcelRuleConfig {

	private final static String FILE_PATH_CONFIG = "/excel-write-rule.xml";

	private static String excelXPath = "excel-component/excel";
	private static String ruleNameXPath = "/ruleName";
	private static String excleNameXPath = "/excelName";
	private static String sheetNameXPath = "/sheetName";
	private static String classNameXPath = "/className";
	private static String columnHeadXPath = "/columnHead";
	private static String columnAttributeXPath = "/columnAttribute";
	private static String column = "column";
	private static String columnAttributeWidth = "width";
	private final static Map<String, ExcelRule> EXCEL_RULES = new HashMap<String, ExcelRule>();

	static {
		try {
			InputStream is = Thread.class.getClass().getResourceAsStream(FILE_PATH_CONFIG);
			Document document = XmlUtils.loadStream(is);
			NodeList excelNodeList = XmlUtils.searchForNodeList(document, excelXPath);
			int excelNodeListLength = excelNodeList.getLength();
			ExcelRule excelRule = null;
			String ruleName = null;
			String excelName = null;
			String sheetName = null;
			String className = null;
			String headValue = null;
			String attributeValue = null;
			Node columnHead = null;
			Node widthNode = null;
			Node columnHeadAttribute = null;
			NodeList columnHeadList = null;
			Node columnAttribute = null;
			NodeList columnAttributeList = null;
			Column columnEntity = null;
			for (int i = 1; i <= excelNodeListLength; i++) {
				excelRule = new ExcelRule();
				ruleName = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]" + ruleNameXPath).getTextContent();
				excelName = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]" + excleNameXPath).getTextContent();
				sheetName = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]" + sheetNameXPath).getTextContent();
				className = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]" + classNameXPath).getTextContent();
				columnHead = XmlUtils.searchForNode(document,excelXPath + "[" + i + "]" + columnHeadXPath);
				columnHeadList = columnHead.getChildNodes();
				for(int ci = 0 ; ci < columnHeadList.getLength() ; ci ++) {
					if(columnHeadList.item(ci).getNodeName().contains(column)) {
						columnEntity = new Column();
						columnHeadAttribute = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]" + columnHeadXPath + "/" + columnHeadList.item(ci).getNodeName());
						headValue = columnHeadAttribute.getTextContent();
						widthNode = columnHeadAttribute.getAttributes().getNamedItem(columnAttributeWidth);
						if(widthNode == null) {
							columnEntity.setWidth(60);
						}else {
							columnEntity.setWidth(Integer.parseInt(widthNode.getTextContent()));
						}
						columnEntity.setName(headValue);
						excelRule.getColumnHeadNameConfig().add(columnEntity);
					}
				}
				columnAttribute = XmlUtils.searchForNode(document,excelXPath + "[" + i + "]" + columnAttributeXPath);
				columnAttributeList = columnAttribute.getChildNodes();
				for(int ci = 0; ci < columnAttributeList.getLength(); ci ++) {
					if(columnAttributeList.item(ci).getNodeName().contains(column)) {
						attributeValue = XmlUtils.searchForNode(document, excelXPath + "[" + i + "]"  + columnAttributeXPath + "/" + columnAttributeList.item(ci).getNodeName()).getTextContent();
						excelRule.getColumnAttributeNameConfig().add(attributeValue);
					}
					
				}
				excelRule.setRuleName(ruleName);
				excelRule.setExcelName(excelName);
				excelRule.setSheetName(sheetName);
				excelRule.setClassName(className);		
				EXCEL_RULES.put(ruleName, excelRule);
			}
		} catch (Exception e) {
			throw new ExcelRuleParseException(e);
		}
	}
	
	public static Map<String,ExcelRule> getAllExcelRule(){
		return Collections.unmodifiableMap(EXCEL_RULES);
	}
	
	public static ExcelRule getExcelRule(String ruleName) {
		return EXCEL_RULES.get(ruleName);
	}

}
